package org.dream.ui.importui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;
import org.dream.persistence.PersistenceService;
import org.dream.persistence.PersistenceServiceImpl;
import org.dream.service.ImportService;
import org.dream.service.ImportServiceImpl;
import org.dream.service.StockService;
import org.dream.service.StockServiceImpl;
import org.dream.ui.JTableUtil;

public abstract class ImportPanel extends JPanel {

	JTextField fileText = new JTextField("", 10);

	protected JTextArea result = new JTextArea(30, 60);
	JFileChooser jfileChooser = new JFileChooser();// 文件选择器
	static final String DEFAULT_IMPORT_FILEPATH = "d:/";

	protected StockService stockService = new StockServiceImpl();
	protected ImportService importService = new ImportServiceImpl();
	protected PersistenceService persistenceService = new PersistenceServiceImpl();

	public ImportPanel() {

		// 下面设定标签等的出现位置和高宽
		JLabel selectLabel = new JLabel("选择目录");
		selectLabel.setBounds(10, 10, 70, 20);
		fileText.setBounds(80, 10, 120, 20);

		this.add(selectLabel);
		this.add(fileText);
		/** 文件选择 */
		jfileChooser.setCurrentDirectory(new File(DEFAULT_IMPORT_FILEPATH));// 文件选择器的初始目录定为d盘

		addFileChooserButton();// 添加文件选择

		addImportButton();// 添加导入按钮

		addDelButton();// 清库
		this.add(new JScrollPane(result));
		result.setFont(JTableUtil.TABLE_FONT);
	}

	public abstract String getTitle();

	private void addFileChooserButton() {
		// 添加文件选择
		final JButton fileButton = new JButton("...");
		fileButton.setBounds(210, 10, 50, 20);

		fileButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {// 事件处理
				if (e.getSource().equals(fileButton)) {// 判断触发方法的按钮是哪个
					jfileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// 设定只能选择到文件夹
					int state = jfileChooser.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
					if (state == 1) {
						return;// 撤销则返回
					} else {
						File f = jfileChooser.getSelectedFile();// f为选择到的目录
						fileText.setText(f.getAbsolutePath());
						// 提示信息
						addPromptInfo();
					}
				}
			}
		});
		this.add(fileButton);
	}

	/**
	 * 添加清空数据库按钮
	 */
	private void addDelButton() {
		JButton delButton = new JButton("清空数据库");
		delButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {// 事件处理
				clearDb();
			}

		});
		this.add(delButton);
	}

	public abstract void clearDb();

	/**
	 * 添加导入数据按钮
	 */
	private void addImportButton() {
		JButton importButton = new JButton("导入");
		importButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {// 事件处理
				if (StringUtils.isEmpty(fileText.getText())) {

					result.setText("请先选择文件");
					return;
				}
				onClickImportButton();
			}

		});
		this.add(importButton);
	}

	public abstract void onClickImportButton();

	/**
	 * 添加导入项目目录大盘
	 */
	protected void addPromptInfo() {

		result.setText("");
	}

}
